package com.opcode.nebulamobil.base.components;
import com.opcode.nebulamobil.base.interfaces.OnButtonBaseClickListener;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * Kullanılacak butonlar ile ilgili ortak yapılması istenilen işler buradan yönetilecektir.
 * @author Okan ERDO�AN
 * 28/05/2014
 *
 */
public class ButtonBase extends Button implements OnClickListener{

	public static volatile long lastPressedTime=0;
	private OnButtonBaseClickListener onButtonBaseClickListener;
	private boolean isWaiting=true;
	
	public ButtonBase(Context context) {
		super(context);
		this.setOnClickListener(this);
	}

	public ButtonBase(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setOnClickListener(this);
	}	

	public ButtonBase(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setOnClickListener(this);
	}
	
	public void setWaitingState(boolean isWaiting) {
		this.isWaiting=isWaiting;
	}

	@Override
	public void onClick(View v) {
		if(this.isWaiting) {
			long time=System.currentTimeMillis();
			if((time-lastPressedTime)>1500){
				lastPressedTime=time;	
				if(getOnButtonBaseClickListener()!=null){
					getOnButtonBaseClickListener().onButtonBaseClick(v);
				}
			}
		}else{
			if(getOnButtonBaseClickListener()!=null){
				getOnButtonBaseClickListener().onButtonBaseClick(v);
			}
		}
	}

	public OnButtonBaseClickListener getOnButtonBaseClickListener() {
		return onButtonBaseClickListener;
	}

	public void setOnButtonBaseClickListener(
			OnButtonBaseClickListener onButtonBaseClickListener) {
		this.onButtonBaseClickListener = onButtonBaseClickListener;
	}

}
